Page({
  data: {
    currentCity: "",
  },
  onShow() {
    var city = wx.getStorageSync('city')
    console.log(city);
    this.setData({
      currentCity: city,
    })
    wx.getLocation({
      altitude: 'altitude',
      highAccuracyExpireTime: 0,
      isHighAccuracy: true,
      type: 'type',
      success: (res) => {
        console.log(res);
        var {latitude,longitude} =res;
        var key = "5SHBZ-N3D3I-M4IGS-5Y7DV-RQXEQ-SRBNV";
        var url = `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${key}&get_poi=1`;
        wx.request({
          url,
          header: {
            'content-type': 'application/json'
          },
          method: 'GET',
          dataType: 'json',
          responseType: 'text',
          success: (res) => {
            console.log(res);
            var currentCity = res.data.result.address_component.city;
            var storageCity = wx.getStorageSync('city')
            if(currentCity!=storageCity){
              wx.showModal({
                title: '切换城市',
                content: '当时城市不是您所在的城市',
                showCancel: true,
                cancelText: '取消',
                cancelColor: '#000000',
                confirmText: '确定',
                confirmColor: '#3CC51F',
                success: (result) => {
                  if(result.confirm){
                    this.setData({
                      currentCity
                    })
                    wx.setStorageSync('city', currentCity);
                  }else{
                    this.setData({
                      currentCity:storageCity,
                    })
                  }
                },
                fail: ()=>{},
                complete: ()=>{}
              });
            }
            
          }
        })
      },
    })
  },

  OnClick(e) {
    wx.navigateTo({
      url: '../../pages/city/city',
    })
  }
})